package com.zxt.dental_sys.Controller.DoctorInfo;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zxt.dental_sys.model.DoctorInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.DoctorInfoServiceImpl;
import com.zxt.dental_sys.service.impl.SysElementUserinfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/doctor/info")
public class DoctorInfoController {

    @Resource
    DoctorInfoServiceImpl doctorInfoService;

    @Resource
    JwtUtil jwtUtil;

    @Resource
    SysElementUserinfoServiceImpl userinfoService;

    @RequestMapping("/apply")
    public RestBean apply(@RequestBody DoctorInfo doctorInfo, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        try {
            DoctorInfo doctorInfo1 = doctorInfoService.getOne(new QueryWrapper<DoctorInfo>()
                    .eq("user_id", uuid)
            );
            doctorInfo1.setDepartments(doctorInfo.getDepartments());
            doctorInfo1.setFields(doctorInfo.getFields());
            doctorInfo1.setJobTitle(doctorInfo.getJobTitle());
            doctorInfo1.setQualificationCertificate(doctorInfo.getQualificationCertificate());
            doctorInfo1.setStatus(1);
            return doctorInfoService.updateById(doctorInfo1)?
                    RestBean.success(doctorInfo1):RestBean.failure(500,"服务异常");
        }catch (Exception e){
            doctorInfoService.remove(new QueryWrapper<DoctorInfo>()
                    .eq("user_id",uuid)
            );//防重
            doctorInfo.setDoctorId(UUID.randomUUID().toString());
            doctorInfo.setStatus(1);
            doctorInfo.setDoctorId(uuid);
            return doctorInfoService.save(doctorInfo)?
                    RestBean.success(doctorInfo):RestBean.failure(500,"服务异常");
        }
    }

    @RequestMapping("info")
    public RestBean getDoctorInfo(HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        if (uuid == null || uuid.isEmpty()){
            return RestBean.failure(400,"用户信息异常");
        }
        try {
            DoctorInfo byId = doctorInfoService.getOne(new QueryWrapper<DoctorInfo>()
                    .eq("user_id",uuid)
            );
            if(byId == null){
                throw new Exception();
            }
            return RestBean.success(byId);
        }catch (Exception e){
            DoctorInfo doctorInfo = new DoctorInfo();
            doctorInfo.setDoctorId(UUID.randomUUID().toString());
            doctorInfo.setStatus(0);
            doctorInfo.setUserId(uuid);
            return doctorInfoService.save(doctorInfo)?
                    RestBean.success(doctorInfo):RestBean.failure(500,"服务异常");
        }
    }

    @RequestMapping("/list/{page}/{size}")
    public RestBean list(@PathVariable int page,@PathVariable int size){
        PageMethod.startPage(page,size);
        List<DoctorInfo> doctorInfos = doctorInfoService.list();
        doctorInfos.forEach(el->{
            el.setUserId(JSON.toJSONString(userinfoService.getById(el.getUserId())));
        });
        return RestBean.success(new PageInfo(doctorInfos));
    }




}
